const AlipayFormData = require("alipay-sdk/lib/form").default;
const uuid = require("uuid");

const alipaySdk = require("../rsa/alipay.rsa");

const orderModel = require("../sequelize/order");
const productModel = require("../sequelize/product");
const productImageModel = require("../sequelize/productImage");
const productLabelModel = require("../sequelize/productLabel");
const userModel = require("../sequelize/user");

class Service {
    async list() {
        return {
            code: 200,
            message: "ok",
            result: await orderModel.findAll({
                include: [
                    {
                        model: productModel,
                        include: [
                            {
                                model: productImageModel,
                            },
                            {
                                model: productLabelModel,
                            },
                        ],
                    },

                    {
                        model: userModel,
                    },
                ],
            }),
        };
    }

    async add(body) {
        if (body.orders) {
            let price = 0;
            for (let i = 0; i < body.orders.length; i++) {
                await orderModel.create(body.orders[i]);
                price += body.orders[i].price;
            }

            const formData = new AlipayFormData();
            formData.setMethod("get");

            formData.addField("bizContent", {
                outTradeNo: uuid.v4(), // 商户订单号,64个字符以内、可包含字母、数字、下划线,且不能重复
                productCode: "FAST_INSTANT_TRADE_PAY", // 销售产品码，与支付宝签约的产品码名称,仅支持FAST_INSTANT_TRADE_PAY
                totalAmount: parseFloat(price).toFixed(2), // 订单总金额，单位为元，精确到小数点后两位
                subject: "订单", // 订单标题
                body: "", // 订单描述
            });

            formData.addField("returnUrl", body.returnUrl); //加在这里才有效果,不是加在bizContent 里面

            const result = await alipaySdk.exec(
                "alipay.trade.page.pay",
                {},
                { formData }
            );

            return {
                code: 200,
                message: "ok",
                result,
            };
        } else {
            await orderModel.create(body);

            const formData = new AlipayFormData();
            formData.setMethod("get");

            formData.addField("bizContent", {
                outTradeNo: uuid.v4(), // 商户订单号,64个字符以内、可包含字母、数字、下划线,且不能重复
                productCode: "FAST_INSTANT_TRADE_PAY", // 销售产品码，与支付宝签约的产品码名称,仅支持FAST_INSTANT_TRADE_PAY
                totalAmount: parseFloat(body.price).toFixed(2), // 订单总金额，单位为元，精确到小数点后两位
                subject: "订单", // 订单标题
                body: "", // 订单描述
            });

            formData.addField("returnUrl", body.returnUrl); //加在这里才有效果,不是加在bizContent 里面

            const result = await alipaySdk.exec(
                "alipay.trade.page.pay",
                {},
                { formData }
            );

            return {
                code: 200,
                message: "ok",
                result,
            };
        }
    }

    async update(body) {
        await orderModel.update(body, {
            where: {
                id: body.id,
            },
        });

        return {
            code: 200,
            message: "ok",
            result: body,
        };
    }

    async del(body) {
        await orderModel.destroy({
            where: {
                id: body.id,
            },
        });
        return {
            code: 200,
            message: "ok",
            result: body,
        };
    }
}

module.exports = new Service();
